package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.CodeSort;
import com.prince.page.Page;
import com.prince.web.base.BaseController;
import com.prince.web.service.CodeSortService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("unchecked")
public class CodeSortController extends BaseController {

	@Resource
	private CodeSortService codeSortService;

	/**
	 * 分页查询
	 * 
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findCodeSortByPage", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> findCodeSortByPage(@RequestParam Map<String, Object> params) {
		Page page = getRequestPageRollInfo(params);
		Map<String, Object> map = codeSortService.findCodeSortByPage(params, page);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}

	/**
	 * 添加
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "addCodeSort", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> addCodeSort(HttpServletRequest request,CodeSort codeSort) {
		Map<String, Object> map = codeSortService.addCodeSort(codeSort);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}

	/**
	 * 修改
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "updateCodeSort", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> updateCodeSort(CodeSort codeSort) {
		Map<String, Object> map = codeSortService.updateCodeSort(codeSort);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}

	/**
	 * 删除
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "delCodeSort", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> delCodeSort(CodeSort codeSort) {
		Map<String, Object> map = codeSortService.delCodeSort(codeSort);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}

	/**
	 * 根据条件查询department
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findCodeSortByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> findCodeSortByCondition(CodeSort codeSort) {
		Map<String, Object> map = codeSortService.findCodeSortByCondition(codeSort);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}
	/**
	 * 根据条件模糊查询department
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "dqueryCodeSort", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> dqueryCodeSort(CodeSort codeSort) {
		Map<String, Object> map = codeSortService.dqueryCodeSort(codeSort);
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
	}

	/**
	 * 根据条件查询code
	 * 
	 * @param codeSort
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findSortNameByCondition", method = RequestMethod.POST)
	public ResponseEntity<String> findSortNameByCondition(CodeSort codeSort) {
		String name = "无";
		if (null == codeSort.getCode() || codeSort.getCode().equals("")) {
			codeSort.setCode(" ");
		}

		List<CodeSort> list = (List<CodeSort>) codeSortService.findCodeSortByCondition(codeSort).get("list");
		if (null != list && list.size() > 0) {

			name = list.get(0).getName();
		}
		return new ResponseEntity<String>(name, HttpStatus.OK);
	}

	/**
	 * 根据条件查询code
	 * 
	 * @param codeSort
	 * @return 返回list code
	 */
	@AppLog
	@RequestMapping(value = "findCodeSortListByCondition", method = RequestMethod.POST)
	public ResponseEntity<List<CodeSort>> findCodeSortListByCondition(CodeSort codeSort) {
		
		List<CodeSort> list = (List<CodeSort>) codeSortService.findCodeSortByCondition(codeSort).get("list");
		return new ResponseEntity<List<CodeSort>>(list, HttpStatus.OK);
	}

	/**
	 * 根据条件查询code
	 * 
	 * @param codeSort
	 * @return 返回Map code
	 */
	@AppLog
	@RequestMapping(value = "findCodeSortMapByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findCodeSortMapByCondition(CodeSort codeSort) {
		List<CodeSort> list = (List<CodeSort>) codeSortService.findCodeSortByCondition(codeSort).get("list");
		Map<String,Object> map = new HashMap<>();
		for (CodeSort codeSort2 : list) {
			map.put(codeSort2.getCode(), codeSort2.getName());
		}
		return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
	}

}
